package tinglema.server.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import tinglema.server.common.R;
import tinglema.server.entity.domain.ListSong;
import tinglema.server.entity.domain.Song;
import tinglema.server.service.ListSongService;
import tinglema.server.service.SongService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 歌曲表(Song)表控制层
 *
 * @author delucia
 * @since 2025-03-20 20:44:05
 */
@RestController
@RequestMapping("song")
public class SongController {
    /**
     * 服务对象
     */
    @Resource
    private SongService songService;

    @Resource
    private ListSongService listSongService;

    /**
     * 返回所有歌曲
     *
     * @return 所有歌曲
     */
    @GetMapping
    public R getAllSong() {
        return R.success(this.songService.list());
    }


    // song/detail?id=${id}
    @GetMapping("/detail")
    public R getSongOfId(@RequestParam Long id) {
        return R.success(this.songService.getById(id));
    }



    /**
     * 返回指定歌手ID的歌曲
     *
     * @param singerId 歌手ID
     * @return 指定歌手ID的歌曲
     */
    @GetMapping("/singer/detail")
    public R getSongOfSingerId(@RequestParam Long singerId) {
        return R.success(this.songService.list(new LambdaQueryWrapper<Song>()
                .eq(Song::getSingerId, singerId)));
    }

    /**
     * 根据歌手ID返回歌曲列表
     *
     * @param id 用户ID
     * @return 指定用户ID的歌曲列表
     */
    @GetMapping("/listBySingerId")
    public R getSongListOfUserId(@RequestParam Long id) {
        return R.success(this.songService.list(new LambdaQueryWrapper<Song>()
                .eq(Song::getSingerId, id)));
    }

    // 客户端 API

    @GetMapping("/like")
    public R getSongOfNameLike(@RequestParam String name) {
        return R.success(this.songService.list(new LambdaQueryWrapper<Song>()
                .like(Song::getName, name)));
    }

    // 返回指定歌单 ID 的所有歌曲
    @GetMapping("songList/{songListId}")
    public R songOfSongListId(@PathVariable("songListId") int songListId) {
        // 查询指定歌单 ID 的所有歌曲
        List<ListSong> listSongs = this.listSongService.list(new LambdaQueryWrapper<ListSong>()
                .eq(ListSong::getSongListId, songListId));

        // 提取歌曲 ID 列表
        List<Integer> songIds = listSongs.stream()
                .map(ListSong::getSongId)
                .collect(Collectors.toList());

        // 查询歌曲列表
        List<Song> songs = this.songService.listByIds(songIds);

        return R.success("查询成功", songs);
    }
}